package com.gzsxy.esjy.gateway.service;


import com.gzsxy.esjy.gateway.dto.LoginDto;
import com.gzsxy.esjy.gateway.dto.LoginReadyResultDto;
import com.gzsxy.esjy.gateway.dto.MenuTreeDto;
import com.gzsxy.esjy.gateway.sso.dto.DefaultUser;
import com.gzsxy.esjy.redis.user.ISystemUser;
import com.gzsxy.esjy.service.base.dto.request.UserRequest;
import org.springframework.http.HttpHeaders;

import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;

import java.util.List;

public interface GlobalService {

    /**
     * @description: 登录准备
     * @param
     * @return
     */
    LoginReadyResultDto loginReady(String username, String loginId);

    /**
     * @description: 登录
     * @param
     * @return
     */
    DefaultUser login(LoginDto loginDto, ServerWebExchange exchange);


    /**
     * @description: 直接登录
     * @param
     * @return
     */
    DefaultUser login(String username,String password,String loginId, ServerWebExchange exchange);


    /**
     * @description: 获取登录用户
     * @param
     * @return
     */
    ISystemUser getLoginUser(String access_token,ServerWebExchange exchange);

    /**
     * @description: 退出登录
     * @param  access_token token
     * @return
     */
    void logout(String access_token);


    /**
     * @description: 刷新token
     * @param
     * @return
     */
    ISystemUser refresh(String access_token,String refresh_token,ServerWebExchange exchange);


    /**
     * @description: 获取用户菜单
     * @param
     * @return
     */
    List<MenuTreeDto> getUserMenu(String accessToken, String appCode, ServerWebExchange exchange);

    /**
     * @description: 获取客户端访问ip地址
     * @param
     * @return
     */
    default String getIpAddress(ServerHttpRequest request){
        HttpHeaders headers = request.getHeaders();
        String ip = headers.getFirst("x-forwarded-for");
        //注意本地测试时，浏览器请求不要用localhost，要用本机IP访问项目地址，不然这里取不到ip
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)){
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            if (ip.indexOf(",") != -1){
                ip = ip.split(",")[0];
            }
        }

        //代理客户端的IP
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = headers.getFirst("Proxy-Client-IP");
        }
        // 是在Weblogic下获取真实IP所用的的参数
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = headers.getFirst("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = headers.getFirst("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = headers.getFirst("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = headers.getFirst("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddress().getAddress().getHostAddress();
        }
        return ip;
    }


    /**
     * @description: 商家审核成功注册账户
     * @param
     * @return
     */
    void verify(String accessToken, ServerWebExchange exchange,UserRequest userRequest);


    /**
     * @description: 获取当前系统的所有商家id
     * @param
     * @return
     */
    void selectAllUserId();
}
